package com.sugon.modules.tools.service.impl;

import com.sugon.dao.ToolsListDao;
import com.sugon.entity.ToolsDemandPlanEntity;
import com.sugon.entity.ToolsDemandPlanItemEntity;
import com.sugon.modules.base.consts.SysApproveStatusConst;
import com.sugon.modules.base.model.dto.HandleApproveResult;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.base.service.impl.AbstractErmsBaseApproveManager;
import com.sugon.modules.sys.consts.ProcessProgressStatus;
import com.sugon.modules.tools.service.IToolsNeedsPlanManager;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.service.ToolsDemandPlanItemService;
import com.sugon.service.ToolsDemandPlanService;
import com.sugon.utils.ProcessInfoEnum;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ： YuXD
 * @description： 备件需求计划Manager实现类
 * @date ： 2020-08-29 下午 5:32
 * @version: 1.0
 */
@Service
public class ToolsNeedsPlanManagerImpl extends AbstractErmsBaseApproveManager implements IToolsNeedsPlanManager {

    @Autowired
    private ToolsDemandPlanService toolsDemandPlanService;
    @Autowired
    private SysProcessInfoService processInfoService;
    @Autowired
    private SysMsgService msgService;
    @Autowired
    private ToolsDemandPlanItemService toolsDemandPlanItemService;
    @Autowired
    private ToolsListDao toolsListDao;

    /**
     * handleWorkshopApprove
     *
     * @param approveItemId 审批事项ID
     * @param approveStatus 审批状态
     * @param remark        审批备注
     * @return
     */
    @Override
    public HandleApproveResult handleWorkshopApprove(String approveItemId, Integer approveStatus, String remark) {
        HandleApproveResult handleApproveResult = new HandleApproveResult();
        ToolsDemandPlanEntity sparePartNeeds = toolsDemandPlanService.queryObject(approveItemId);
        org.springframework.util.Assert.notNull(sparePartNeeds, "备件需求不存在，请确认！");
        // 校验规程进度
        org.springframework.util.Assert.isTrue(ProcessProgressConst.DEPT_APPROVING.equals(sparePartNeeds.getProgress()), "只可以审核处于审核中的备件需求！");
        handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
        handleApproveResult.setTitle(sparePartNeeds.getPlanCode());
        // 如果审核通过
        if (approveStatus == SysApproveStatusConst.PASSED) {
            sparePartNeeds.setProgress(ProcessProgressConst.CJ_PE_APPROVING);
            int rows = toolsDemandPlanService.update(sparePartNeeds);
            if (rows > 0) {
                handleApproveResult.setNextNodeLevel(1);
            }
        } else if (approveStatus == SysApproveStatusConst.NOT_PASSED) {
            sparePartNeeds.setProgress(ProcessProgressConst.NOT_PASSED);
            int rows = toolsDemandPlanService.update(sparePartNeeds);
            if (rows > 0) {
                handleApproveResult.setUserId(sparePartNeeds.getOrganUser());
            }
        }
        return handleApproveResult;
    }

    /**
     * handlePEApprove
     *
     * @param approveItemId 审批事项ID
     * @param approveStatus 审批状态
     * @param remark        审批备注
     * @return
     */
    @Override
    public HandleApproveResult handlePEApprove(String approveItemId, Integer approveStatus, String remark) {
        HandleApproveResult handleApproveResult = new HandleApproveResult();
        ToolsDemandPlanEntity sparePartNeeds = toolsDemandPlanService.queryObject(approveItemId);
        org.springframework.util.Assert.notNull(sparePartNeeds, "备件需求不存在，请确认！");
        // 校验规程进度
        org.springframework.util.Assert.isTrue(ProcessProgressConst.CJ_PE_APPROVING.equals(sparePartNeeds.getProgress()), "只可以审核处于审核中的备件需求！");
        handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
        handleApproveResult.setTitle(sparePartNeeds.getPlanCode());
        // 如果审核通过
        if (approveStatus == SysApproveStatusConst.PASSED) {
            sparePartNeeds.setProgress(ProcessProgressConst.ZZC_PE_APPROVING);
            int rows = toolsDemandPlanService.update(sparePartNeeds);
            if (rows > 0) {
                handleApproveResult.setNextNodeLevel(2);
            }
        } else if (approveStatus == SysApproveStatusConst.NOT_PASSED) {
            sparePartNeeds.setProgress(ProcessProgressConst.NOT_PASSED);
            int rows = toolsDemandPlanService.update(sparePartNeeds);
            if (rows > 0) {
                handleApproveResult.setUserId(sparePartNeeds.getOrganUser());
            }
        }
        return handleApproveResult;
    }

    /**
     * handlePE2Approve
     *
     * @param approveItemId 审批事项ID
     * @param approveStatus 审批状态
     * @param remark        审批备注
     * @return
     */
    @Override
    public HandleApproveResult handlePE2Approve(String approveItemId, Integer approveStatus, String remark) {
        HandleApproveResult handleApproveResult = new HandleApproveResult();
        ToolsDemandPlanEntity sparePartNeeds = toolsDemandPlanService.queryObject(approveItemId);
        org.springframework.util.Assert.notNull(sparePartNeeds, "备件需求不存在，请确认！");
        // 校验规程进度
        org.springframework.util.Assert.isTrue(ProcessProgressConst.ZZC_PE_APPROVING.equals(sparePartNeeds.getProgress()), "只可以审核处于审核中的备件需求！");

        handleApproveResult.setTitle(sparePartNeeds.getPlanCode());
        // 如果审核通过
        if (approveStatus == SysApproveStatusConst.PASSED) {
            sparePartNeeds.setProgress(ProcessProgressConst.PASSED);
            int rows = toolsDemandPlanService.update(sparePartNeeds);
            if (rows > 0) {
                handleApproveResult.setNextNodeLevel(2);
                handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.END);
            }
        } else if (approveStatus == SysApproveStatusConst.NOT_PASSED) {
            sparePartNeeds.setProgress(ProcessProgressConst.NOT_PASSED);
            int rows = toolsDemandPlanService.update(sparePartNeeds);
            if (rows > 0) {
                handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
                handleApproveResult.setUserId(sparePartNeeds.getOrganUser());
            }
        }
        return handleApproveResult;
    }

    /**
     * commitApprove
     *
     * @param toolsDemandPlan 工装需求计划
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean commitApprove(ToolsDemandPlanEntity toolsDemandPlan) {
        toolsDemandPlan.setProgress(ProcessProgressConst.DEPT_APPROVING);
        boolean flag = toolsDemandPlanService.update(toolsDemandPlan) > 0;
        if (flag) {
            String sparePartNeedsPlanId = toolsDemandPlan.getId();
            // 保存流程信息
            processInfoService.saveProcessInfo(sparePartNeedsPlanId, ProcessInfoEnum.SPA_BJXQ, 0, null);
            // 添加备件审批事项
            msgService.addSparePartNeedsPlanApproveItem(sparePartNeedsPlanId, 0);
        }
        return flag;
    }

    /**
     * cancelTransit
     *
     * @param toolsDemandPlanItem 工装需求计划条目详情
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean cancelTransit(ToolsDemandPlanItemEntity toolsDemandPlanItem) {
        toolsDemandPlanItem.setIsCancelTransit(1);
        toolsDemandPlanItem.setTransitNum(0);
        boolean flag = toolsDemandPlanItemService.update(toolsDemandPlanItem) > 0;
        if (flag) {
            // 更新下备件在途数
            toolsListDao.realtimeUpdateTransitNum(toolsDemandPlanItem.getMaterialId());
        }
        return flag;
    }

    /**
     * reduceTransitNum
     *
     * @param sparePartNeedsDetailId 工装需求计划明细项ID
     * @param num                    数目
     * @return
     */
    @Override
    public boolean reduceTransitNum(String sparePartNeedsDetailId, int num) {
        // 校验备件需求计划条目项是否存在
        ToolsDemandPlanItemEntity sparePartRequDetail = toolsDemandPlanItemService.queryObject(sparePartNeedsDetailId);
        Assert.notNull(sparePartRequDetail, "备件需求计划条目项不存在");
        // 校验入库数不得大于在途数
        int transitNum = sparePartRequDetail.getTransitNum();
        Assert.isTrue(transitNum >= num, "入库数不得大于在途数");
        // 修改入库和在途数
        sparePartRequDetail.setInboundNum(sparePartRequDetail.getInboundNum() + num);
        sparePartRequDetail.setTransitNum(transitNum - num);
        boolean flag = toolsDemandPlanItemService.update(sparePartRequDetail) > 0;
        if (flag) {
            // 更新下备件在途数
            toolsListDao.realtimeUpdateTransitNum(sparePartRequDetail.getMaterialId());
        }
        return flag;
    }
}
